﻿$(document).ready(function() {
    StartUploadify();
    
});
var iMaxUpdateFileSize = 1024 * 1024 * 800; //允许最大文件800MB
function StartUploadify() {
    $("#"+ JQFileUpLoadName +"_FileUpload1").uploadify({
        'uploader': '/JQueryContrls/JqueryUploadify/uploadify2.swf',   //默认选择按钮图片
        'cancelImg': '/JQueryContrls/JqueryUploadify/images/cancel.gif',  //取消按钮图片
        'buttonText': 'Select',                                        //按钮文本
        'buttonImg': '/JQueryContrls/JqueryUploadify/images/selectFujian.gif', //选择按钮图片
        'width': 100,
        'height': 25,
        'script': '/JQueryContrls/JqueryUploadify/AspNetCommUpload.aspx?UpLoadType=mail', //上传文件的类,该处引用邮件附件上传规则
        'folder': 'uploads',
        'fileDesc': 'Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png;*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.txt;*.rar;*.zip;*.bmp;*.tif', //允许上传的文件类型
        'sizeLimit': iMaxUpdateFileSize, /*B为单位*/
        'simUploadLimit': 10,  /*多文件上传时，同时上传文件数目限制*/
        'multi': true,         /*是否允许同时上传多文件，默认false*/
        'auto': true,
        'onSelect': function(a, b, c) { /*选择文件上传时可以禁用某些按钮*/ },
        'onComplete': function(event, queueID, fileObj, response, data) { SetLi_UpdateFiles(fileObj, response); }, //上传后的事件
        'onAllComplete': function(a, b) {
            enabledSaveButton(true);
        },
        'onCancel': function(a, b, c, d, e) { },
        'onError': function(a, b, c, d, e) {
            if (c.size > iMaxUpdateFileSize) {
                setTimeout("$('#"+ JQFileUpLoadName +"_FileUpload1').uploadifyCancel('" + b + "')", 2000);
            }
        }
    });
}

function SetLi_UpdateFiles(objFile, response) {
    var strresponse = response.substring(0, 1);
    switch (strresponse) {
        case "1":
            var iNextLiIndex = parseInt($("#"+ JQFileUpLoadName +"_hd_Index").val(), 10);
            var sLi = "<li id=\"_file-" + iNextLiIndex.toString() + "\" class=\"file\">";
            sLi += "<span>" + objFile.name + "</span>";
            sLi += "<span>(" + GetFileSize(objFile.size) + ")</span>";
            sLi += " <a href=\"javascript:DeleteFile(1,'_file-" + iNextLiIndex.toString() + "','','" + response.substring(1, response.length) + "');\"><img src=\"/JQueryContrls/JqueryUploadify/images/FileDel.gif\" width='16' height='16' align=\"middle\" border=\"0\" /></a></li>";
            $("#"+ JQFileUpLoadName +"_hd_Index").val(iNextLiIndex + 1);
            $(sLi).appendTo($("#demo-list"));

            if ($("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val() != "") {
                $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val($("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val() + "#");
            }
            $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val($("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val() + response.substring(1, response.length) + "|" + objFile.size + "_file-" + iNextLiIndex.toString());

            showResult("文件上传成功!"); //显示上传成功结果
            break;
        case "0":
            showResult("文件上传失败!"); //显示上传失败结果
            break;
        default:
            showResult(response);  //显示上传错误消息
            break;

    }
    setInterval("delshowResult()", 3000); //三秒后删除 显示结果

}

function showResult(Result) {//删除显示结果
    var showLi = "<li id=\"result\"><span>" + Result + "</span></li>";
    $(showLi).appendTo($("#demo-list"));
}
function delshowResult() {//删除显示结果
    $("#result").remove();
}

function GetFileSize(iBytes) {
    var iFileKB = iBytes / 1024;
    iFileKB = SizeFormat(iFileKB.toString());
    return iFileKB + " KB";
}

function SizeFormat(s) {
    s = s.replace(/^(\d*)$/, "$1.");
    s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
    s = s.replace(".", ",");
    var re = /(\d)(\d{3},)/;
    while (re.test(s))
        s = s.replace(re, "$1,$2");
    s = s.replace(/,(\d\d)$/, ".$1");
    return s.replace(/^\./, "0.")
}

function DeleteFile(b, liId, sDeleteId, sDeleteFileName) {

    //从显示列表中移除
    $("#" + liId).remove();
    //删除处理
    DeleteUpdateFileValue(liId);
    switch (b) {
        case 0: //只删除列表
            break;
        case 1: //除了删除列表，还对隐患文本框加入值
            if ($("#"+ JQFileUpLoadName +"_hd_DeleteFileValue").val() != "") {
                $("#"+ JQFileUpLoadName +"_hd_DeleteFileValue").val($("#"+ JQFileUpLoadName +"_hd_DeleteFileValue").val() + "#");
            }
            $("#"+ JQFileUpLoadName +"_hd_DeleteFileValue").val($("#"+ JQFileUpLoadName +"_hd_DeleteFileValue").val() + sDeleteId + sDeleteFileName);
            break;
    }
    //.NET ajax调用后台页面的DeleteFile删除文件方法
    PageMethods.DeleteFile($("#"+ JQFileUpLoadName +"_hd_DeleteFileValue").val(), getResult);
    $("#"+ JQFileUpLoadName +"_hd_DeleteFileValue").val("");
}
function getResult(Result) {//处理DeleteFile方法返回的值
    alert(Result);
    //$("#"+ JQFileUpLoadName +"_txtFilePathList").val(Result);
}
function DeleteUpdateFileValue(sDeleteValue) {
    //取得上传列表中对应的隐藏文本框中的值
    var sUpdateValue = $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val();
    //删除的文件名称位于元素位置
    var iFindIndex = sUpdateValue.lastIndexOf(sDeleteValue);
    //最靠近sDeleteValue变量值的'#'的元素位置
    var iStartIndex = sUpdateValue.substring(0, iFindIndex).lastIndexOf("#");
    //sDeleteValue变量中最后一个字符的元素位置
    var iEndIndex = iFindIndex + sDeleteValue.length - 1;
    if (iStartIndex == -1) {
        //位于第一组
        var sNewRightValue = $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val().substring(iEndIndex + 2);
        $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val(sNewRightValue);
    }
    else {
        //判断当前删除的是不是属于最后一组
        if ((iEndIndex + 1) == sUpdateValue.length) {
            //位于最后一组，删除最后一个#以后年字符
            $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val($("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val().substring(0, iStartIndex));
        }
        else {
            //在中间
            var sCNewLeftValue = $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val().substring(0, iStartIndex);
            var sCNewRightValue = $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val().substring(iEndIndex + 1);
            $("#"+ JQFileUpLoadName +"_hd_UpdateFileValue").val(sCNewLeftValue + sCNewRightValue);
        }
    }
}

function enabledFileDeleteImg(b) {
    if (b) {
        $("#demo-list").find("a").css("display", "inline");
    }
    else {
        $("#demo-list").find("a").attr("style", "display:none");
    }
}

function yhLoad() {

    $("#demo-list").html($get(""+ JQFileUpLoadName +"_hd_FileList").value);
}